package com.colorful.insuranceproject.consts;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.colorful.insuranceproject.utils.WheelActivity;
import com.lovely3x.common.utils.ALog;
import com.lovely3x.common.utils.Displayable;
import com.lovely3x.common.utils.fileutils.StreamUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lovely3x on 17/3/16.
 *

 *   城市数据
 */
public class CityDataHolder {

    private static final String TAG = "CityDataHolder";

    private static List<Province> sProvince = new ArrayList<Province>();

    public static List<Province> getProvince() {
        return sProvince;
    }

    public static void load(Context context) {
        try {
            InputStream fis = context.getAssets().open("areas.json");
            sProvince = com.alibaba.fastjson.JSON.parseArray(StreamUtils.readToString(fis, "UTF-8"), Province.class);
        } catch (Exception e) {
            ALog.e(TAG, e);
        }
    }

    public static class Province extends WheelActivity.Parent {
        /**
         * name : 吉林省
         * pid : 220000
         * id : 6
         */

        private String name;
        private String pid;
        private int id;

        @com.lovely3x.jsonparser.annotations.JSONArray(object = City.class, value = "city")
        private List<City> city = new ArrayList<City>();

        public Province() {
        }

        @Override
        public List getMiddles() {
            return city;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public String getPid() {
            return pid;
        }

        public int getId() {
            return id;
        }

        @Override
        public String toString() {
            return name;
        }

        public List<City> getCity() {
            return city;
        }

        public void setCity(List<City> city) {
            this.city = city;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeString(this.name);
            dest.writeString(this.pid);
            dest.writeInt(this.id);
            dest.writeTypedList(this.city);
        }

        protected Province(Parcel in) {
            super(in);
            this.name = in.readString();
            this.pid = in.readString();
            this.id = in.readInt();
            this.city = in.createTypedArrayList(City.CREATOR);
        }

        public static final Parcelable.Creator<Province> CREATOR = new Parcelable.Creator<Province>() {
            @Override
            public Province createFromParcel(Parcel source) {
                return new Province(source);
            }

            @Override
            public Province[] newArray(int size) {
                return new Province[size];
            }
        };

    }

    public static class City extends WheelActivity.Middle {
        /**
         * name : 长春市
         * cid : 220100
         */

        private String name;
        private String cid;
        @com.lovely3x.jsonparser.annotations.JSONArray(object = District.class, value = "district")
        private List<District> district;

        public void setName(String name) {
            this.name = name;
        }

        public void setCid(String cid) {
            this.cid = cid;
        }

        public String getName() {
            return name;
        }

        public String getCid() {
            return cid;
        }

        @Override
        public List getChildren() {
            return district;
        }

        @Override
        public String toString() {
            return name;
        }

        public City() {
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public List<District> getDistrict() {
            return district;
        }

        public void setDistrict(List<District> district) {
            this.district = district;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeString(this.name);
            dest.writeString(this.cid);
            dest.writeTypedList(this.district);
        }

        protected City(Parcel in) {
            super(in);
            this.name = in.readString();
            this.cid = in.readString();
            this.district = in.createTypedArrayList(District.CREATOR);
        }

        public static final Parcelable.Creator<City> CREATOR = new Parcelable.Creator<City>() {
            @Override
            public City createFromParcel(Parcel source) {
                return new City(source);
            }

            @Override
            public City[] newArray(int size) {
                return new City[size];
            }
        };
    }

    public static class District extends WheelActivity.Child {

        /**
         * name : 市辖区
         * did : 220101
         */

        private String name;
        private String did;

        public District() {
        }

        public District(Displayable data, String id) {
            super(data,id);
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setDid(String did) {
            this.did = did;
        }

        public String getName() {
            return name;
        }

        public String getDid() {
            return did;
        }

        @Override
        public String toString() {
            return name;
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeString(this.name);
            dest.writeString(this.did);
        }

        protected District(Parcel in) {
            super(in);
            this.name = in.readString();
            this.did = in.readString();
        }

        public static final Parcelable.Creator<District> CREATOR = new Parcelable.Creator<District>() {
            @Override
            public District createFromParcel(Parcel source) {
                return new District(source);
            }

            @Override
            public District[] newArray(int size) {
                return new District[size];
            }
        };
    }


}
